package com.tzbank.common.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tzbank.common.dao.AreaMapper;
import com.tzbank.common.service.AreaService;
import com.tzbank.entity.Area;
import com.tzbank.redis.RedisUtils;
import com.tzbank.utils.CodeUtil;
import com.tzbank.utils.ResultUtil;

import net.sf.json.JSONObject;

@Service
public class AreaServiceImpl implements AreaService {

	@Autowired
	private AreaMapper areaMapper;

	private List<Area> selectDBArea(String id, int level) {

		List<Area> areas = new ArrayList<Area>();

		JSONObject provinceJSON = new JSONObject();
		JSONObject cityJSON = new JSONObject();
		JSONObject countyJSON = new JSONObject();

		List<Area> areasProvince = this.areaMapper.selectAreaByLevel(1);
		provinceJSON.put("000000", areasProvince);
		if (1 == level && "000000".equals(id))
			areas = areasProvince;
		Iterator<Area> iteratorProvince = areasProvince.iterator();
		while (iteratorProvince.hasNext()) {
			Area areaProvince = iteratorProvince.next();
			List<Area> areasCity = this.areaMapper.selectAreaByParent(areaProvince.getId());
			if (2 == level && id.equals(areaProvince.getId()))
				areas = areasCity;
			cityJSON.put(areaProvince.getId(), areasCity);
			Iterator<Area> iteratorCity = areasCity.iterator();
			while (iteratorCity.hasNext()) {
				Area areaCity = iteratorCity.next();
				List<Area> countyCity = this.areaMapper.selectAreaByParent(areaCity.getId());
				if (3 == level && id.equals(areaCity.getId()))
					areas = countyCity;
				countyJSON.put(areaCity.getId(), countyCity);
			}
		}

		JSONObject areaJOSN = new JSONObject();
		areaJOSN.put("province", provinceJSON);
		areaJOSN.put("city", cityJSON);
		areaJOSN.put("county", countyJSON);

		RedisUtils.setKey("area", areaJOSN.toString());

		return areas;
	}

	
	private List<Area> selectRedisArea(String id, int level) {
		String areaJSONstr = RedisUtils.getValue("area");
		if (StringUtils.isEmpty(areaJSONstr)) {
			return this.selectDBArea(id, level);
		} else {
			JSONObject areaJSON = JSONObject.fromObject(areaJSONstr);
			if (1 == level) {
				JSONObject provinceJSON = areaJSON.getJSONObject("province");
				return (List<Area>) provinceJSON.get(id);
			}
			if (2 == level) {
				JSONObject cityJSON = areaJSON.getJSONObject("city");
				return (List<Area>) cityJSON.get(id);
			}
			if (3 == level) {
				JSONObject countyJSON = areaJSON.getJSONObject("county");
				return (List<Area>) countyJSON.get(id);
			}
		}
		return new ArrayList<Area>();
	}

	@Override
	public Map<String, Object> selectArea(String id, int level) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("areas", this.selectRedisArea(id, level));
		return ResultUtil.resultMap(CodeUtil.CODE_200.getName(), CodeUtil.CODE_200.getCode(), jsonObject);
	}

}
